package com.vdg.lockphotos.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.vdg.lockphotos.R;
import com.vdg.lockphotos.data.Photo;
import com.vdg.lockphotos.fragment.HomeFragment;
import com.vdg.lockphotos.notify.OnLoadFileListner;
import com.vdg.lockphotos.util.ImageLoader;
import com.vdg.lockphotos.util.Utilities;

public class ImageSlideAdapter extends PagerAdapter {
	ImageLoader imageLoader;
	FragmentActivity activity;
	List<Photo> products;
	HomeFragment homeFragment;
	private int mWidth;

	public ImageSlideAdapter(FragmentActivity activity, List<Photo> products,
			HomeFragment homeFragment) {
		this.activity = activity;
		this.homeFragment = homeFragment;
		this.products = products;
		imageLoader = ImageLoader.getInstance(activity);
		mWidth = getWidth(activity);
	}

	@Override
	public int getCount() {
		return products.size();
	}

	public static int getWidth(Context activity) {
		WindowManager wm = (WindowManager) activity
				.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		return (int) (Utilities.getDisplaySize(display).x / 2.5);
	}

	@Override
	public View instantiateItem(ViewGroup container, final int position) {
		LayoutInflater inflater = (LayoutInflater) activity
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.vp_image, container, false);

		ImageView mImageView = (ImageView) view
				.findViewById(R.id.image_display);
		imageLoader.DisplayImage(products.get(position).getHiddenUrl(),
				mImageView, new OnLoadFileListner() {

					@Override
					public void onSuccess(boolean isAnimation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onStart() {
						// TODO Auto-generated method stub

					}

					@Override
					public void onFailed() {
						// TODO Auto-generated method stub

					}
				}, mWidth, false);
		container.addView(view);
		return view;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}
}